//
//  MoreTableViewCell.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "MoreTableViewCell.h"

@interface MoreTableViewCell ()
{
    UIImageView * image1;
    UILabel * title1;
    UILabel * time1;
    UIImageView * image2;
    UILabel * title2;
    UILabel * time2;
    UIView * line;
    NSArray * data;
}
@end

@implementation MoreTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 20)];
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        titleLabel.text = @"更多相似活动";
        titleLabel.textColor = [UIColor colorFromHexCode:@"a3a3a3"];
        [self addSubview:titleLabel];
        
        image1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth/2 - 10, ScreenWidth/2 - 10)];
        [self addSubview:image1];
        title1 = [[UILabel alloc]initWithFrame:CGRectMake(10, ScreenWidth/2 + 40, ScreenWidth/2 - 10, 20)];
        title1.font = [UIFont systemFontOfSize:15];
        [self addSubview:title1];
        time1 = [[UILabel alloc]initWithFrame:CGRectMake(10, ScreenWidth/2 + 60, ScreenWidth/2 - 10, 20)];
        time1.font = [UIFont systemFontOfSize:12];
        [self addSubview:time1];
        
        image2 = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 5, 40, ScreenWidth/2 - 10, ScreenWidth/2 - 10)];
        [self addSubview:image2];
        title2 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 5, ScreenWidth/2 + 40, ScreenWidth/2 - 10, 20)];
        title2.font = [UIFont systemFontOfSize:15];
        [self addSubview:title2];
        time2 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 + 5, ScreenWidth/2 + 60, ScreenWidth/2 - 10, 20)];
        time2.font = [UIFont systemFontOfSize:12];
        [self addSubview:time2];
        
        image1.contentMode = UIViewContentModeScaleAspectFill;
        image1.clipsToBounds = YES;
        image2.contentMode = UIViewContentModeScaleAspectFill;
        image2.clipsToBounds = YES;
        
        UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, ScreenWidth/2, ScreenWidth/2 + 10)];
        [button1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button1];
        
        UIButton * button2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2, 40, ScreenWidth/2, ScreenWidth/2 + 10)];
        [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button2];
        
        line = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenWidth/2 + 20 + 30 + 40, ScreenWidth, 5)];
        line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:line];
    }
    return self;
}

+(CGFloat)heightWithData:(NSArray *)data
{
    if ([data count])
    {
        return ScreenWidth/2 + 20 + 30 + 45;
    }
    else
    {
        return 45;
    }
}

-(void)loadData:(NSArray *)data
{
    if ([data count])
    {
        NSDictionary * content1 = [data firstObject];
        NSDictionary * content2 = [data lastObject];
        
        if ([data count] == 1)
        {
            [image1 sd_setImageWithURL:[NSURL URLWithString:[content1 objectForKey:@"TaskPic"]]];
            title1.text = [content1 objectForKey:@"TaskTitle"];
            time1.text = [NSString stringWithFormat:@"%@ %@",[content1 objectForKey:@"TaskBeginTime"],[content1 objectForKey:@"TaskBeginTime"]];
        }
        else
        {
            [image1 sd_setImageWithURL:[NSURL URLWithString:[content1 objectForKey:@"TaskPic"]]];
            [image2 sd_setImageWithURL:[NSURL URLWithString:[content2 objectForKey:@"TaskPic"]]];
            
            title1.text = [content1 objectForKey:@"TaskTitle"];
            title2.text = [content2 objectForKey:@"TaskTitle"];
            
            time1.text = [NSString stringWithFormat:@"%@ %@",[content1 objectForKey:@"TaskBeginTime"],[content1 objectForKey:@"TaskBeginTime"]];
            time2.text = [NSString stringWithFormat:@"%@ %@",[content2 objectForKey:@"TaskBeginTime"],[content2 objectForKey:@"TaskBeginTime"]];
        }
        line.frame = CGRectMake(0, ScreenWidth/2 + 20 + 30 + 40, ScreenWidth, 5);
    }
    else
    {
        line.frame = CGRectMake(0, 40, ScreenWidth, 5);
    }
}

-(void)button1Action
{
    [self.delegate selectedContent:[data objectAtIndex:0]];
}

-(void)button2Action
{
    if ([data count] <= 1)
    {
    }
    else
    {
        [self.delegate selectedContent:[data objectAtIndex:1]];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
